package com.weihua.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class BaseController {
    @ExceptionHandler
    @ResponseBody
    public Map<String, Object> resolveException(Exception ex) {
        Map<String, Object> error = new HashMap<>();
        log.warn(ex.getMessage(), ex);
        error.put("msg",ex.getMessage());
        error.put("success",false);
        return error;
    }

    public Map<String, Object> resolve(Object data){
        return resolve(data,"");
    }
    public Map<String, Object> resolve(Object data,String msg){
        Map<String, Object> result=new HashMap<>();
        result.put("success",true);
        result.put("msg",msg);
        result.put("data",data);
        return result;
    }

    public Map<String, Object> resolveMsg(String msg){
        return this.resolve(null,msg);
    }

    public Map<String, Object> resolveError(String msg, Exception e){
        log.warn(e.getMessage(),e);
        Map<String, Object> result=new HashMap<>();
        result.put("success",false);
        result.put("msg",msg);
        return result;
    }

    public Map<String, Object> resolveError(String msg){
        Map<String, Object> result=new HashMap<>();
        result.put("success",false);
        result.put("msg",msg);
        return result;
    }

}
